package com.qipa.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.AutoMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qipa.entity.BSGridPage;
import com.qipa.service.IBaseService;
import com.qipa.utils.QueryFilter;

import java.util.ArrayList;
import java.util.List;

public class BaseServiceImpl<M extends AutoMapper<T>, T> extends SuperServiceImpl<M, T> implements IBaseService<T> {


    public BSGridPage<T> getBSPage(BSGridPage<T> bsGridPage) {
        return bsGridPage.parsePage(selectPage(bsGridPage.getPage(), null));
    }

    public BSGridPage<T> getBSPage(BSGridPage<T> bsGridPage, QueryFilter queryFilter) {
        Page<T> page1 = bsGridPage.getPage();
        Page<T> page = selectPage(page1, queryFilter.buildEntityWrapper());
        return bsGridPage.parsePage(page);
    }


    public boolean deleteBatchByIds(String ids) {
        String[] idsArray = ids.split(",");
        List<Integer> idList = new ArrayList<Integer>();
        for (String idStr : idsArray) {
            Integer id = Integer.parseInt(idStr);
            idList.add(id);
        }
        return deleteBatchIds(idList);
    }

    public List<T> selectList(T t) {
        EntityWrapper entityWrapper = new EntityWrapper(t);
        return selectList(entityWrapper);
    }

    public List<T> selectList(T t, String orderBy, boolean isAsc) {
        EntityWrapper entityWrapper = new EntityWrapper(t);
        entityWrapper.orderBy(orderBy, isAsc);
        return selectList(entityWrapper);
    }

    public T selectFirst(T t) {
        EntityWrapper<T> entityWrapper = new EntityWrapper<T>(t);
        return selectOne(entityWrapper);
    }
}
